from django.db import models

from goods.models import SKU
from users.models import User, Address

from meiduo_mall.utils.base_model import BaseModel


class OrderInfo(BaseModel):
    """
    订单信息
    """
    PAY_METHOD_CHOICES = (
        (1, '货到付款'),
        (2, '支付宝'),
    )

    STATUS_CHOICES = (
        (1, '待支付'),
        (2, '待发货'),
        (3, '待收货'),
        (4, '待评价'),
        (5, '已完成'),
        (6, '已取消'),
    )

    order_id = models.CharField(verbose_name='订单编号', max_length=64, primary_key=True)
    user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name='下单用户')
    address = models.ForeignKey(Address, on_delete=models.PROTECT, verbose_name='收获地址')
    total_count = models.IntegerField(verbose_name='商品总数', default=1)
    total_amount = models.DecimalField(verbose_name='订单总金额', max_digits=10, decimal_places=2)
    freight = models.DecimalField(verbose_name='运费', max_digits=10, decimal_places=2)
    pay_method = models.IntegerField(choices=PAY_METHOD_CHOICES, verbose_name='支付方式', default=1)
    status = models.IntegerField(choices=STATUS_CHOICES, verbose_name='订单状态', default=1)

    class Meta:
        db_table = 'tb_order_info'
        verbose_name = '订单基本信息'
        verbose_name_plural = verbose_name


class OrderGoods(BaseModel):
    """
    订单商品
    """
    SCORE_CHOICES = (
        (0, '0分'),
        (1, '20分'),
        (2, '40分'),
        (3, '60分'),
        (4, '80分'),
        (5, '100分'),
    )

    order = models.ForeignKey(OrderInfo, related_name='skus' , on_delete=models.CASCADE, verbose_name='订单编号')
    sku = models.ForeignKey(SKU, on_delete=models.PROTECT, verbose_name='商品id')
    count = models.IntegerField(default=1, verbose_name='商品数量')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='单价')
    comment = models.TextField(default='', verbose_name='评论信息')
    score = models.IntegerField(choices=SCORE_CHOICES, default=5, verbose_name='评分')
    is_anonymous = models.BooleanField(default=False, verbose_name='是否匿名评价')
    is_commented = models.BooleanField(default=False, verbose_name='是否评价了')

    class Meta:
        db_table = 'tb_order_goods'
        verbose_name = '订单商品'
        verbose_name_plural = verbose_name
